package net.starwifi.duobao.web;

import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.mvc.Order;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.GglWinningService;
import net.starwifi.duobao.utils.times.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Controller - member.
 *
 * @author ruoo
 * @version 1.0
 */
@Controller("admingglwinningController")
@RequestMapping("/gglwinning")
public class GglWinningController extends BaseController {

   @Autowired
    GglWinningService gglWinningService;
    @Autowired
    private ServletContext servletContext;



//    /**
//     * 编辑
//     */
//    @RequestMapping(value = "/edit", method = RequestMethod.GET)
//    public String edit(Long id, ModelMap model) {
//        model.addAttribute("entity", gglhdService.find(id));
//        return "/gglhd/edit";
//    }
//
//
//    /**
//     * 更新
//     */
//    @RequestMapping(value = "/update", method = RequestMethod.POST)
//    public String update(Gglhd gglhd, RedirectAttributes redirectAttributes) {
//        Gglhd pGglhd=gglhdService.find(gglhd.getId());
//        gglhdService.update(gglhd);
//        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
//        return "redirect:list";
//    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageView pageView, ModelMap model,HttpServletRequest request) {
        pageView.filterQuery(request);
        pageView.addOrder(Order.desc("id"));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model.addAttribute("currentDate",format.format(new Date()));
        Date beginData= DateTool.setHms(new Date(), 0, 0, 0);
        Date endData= DateTool.setHms(new Date(),23,59,59);
        model.addAttribute("beginData", beginData);
        model.addAttribute("endData", endData);
        model.addAttribute("page", gglWinningService.getScrollData(pageView));
        return "/gglwinning/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) {
        gglWinningService.delete(ids);
        return SUCCESS_MESSAGE;
    }
    @RequestMapping(value = "/OnStatus", method = RequestMethod.GET)
    public String OnStatus(Long id, RedirectAttributes redirectAttributes) {
        int status=1;
        gglWinningService.OnStatus(id,status);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 导出Excel
     * */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public String  exportExcel(PageView pageView,HttpServletRequest request,ModelMap model) throws IOException {
        pageView.filterQuery(request);
        pageView.getOrders().add(Order.desc("id"));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String url=gglWinningService.exportExcel2003WithUsermodel(format.format(new Date()), servletContext.getRealPath("/"), pageView);
        model.addAttribute("message","Excel生成成功");
        model.addAttribute("content","<a href='/"+url+"' target=\"_blank\">点击下载</a>");
        return "/common/handle";
    }

}
